function searchParam(param) {
    var searchString = window.location.search.substring(1);
    var params = searchString.split('&');
    for (i = 0; i < params.length; i++) {
        results = params[i].split('=');
        if (results[0] === param) {
            return results[1];
        }
    }
}

$(document).ready(function() {
    if (searchParam('genreCode') !== 'none' || searchParam('publicationTypeCode') === 'OTHER') {
        $(".newBookLink").click(function() {
            $(".newBookForm,.errorMessages").toggle("fast");
        });
    } else {
        $(".newBookLink").css("display", "none");
    }
});

$(document).ready(function() {
    $(".addButton").click(function(event) {
        regexpNumber = /^[0-9]+[^a-zA-Z.,;:|\\\/~!@#$%^&*_-{}\[\]()`"'<>?\s]*$/;
        patternNumber = new RegExp(regexpNumber);

        if (!patternNumber.test($("#bookPages").val()) || $("#bookName").val() === "" || $("#bookAuthor").val() === "" || $("#bookFormat").val() === "") {
            if (!patternNumber.test($("#bookPages").val())) {
                $('#errorBookPages').show().html('Edit a number of pages field');
            }
            if ($("#bookName").val() === "") {
                $('#errorBookName').show().html('Edit a book name filed');
            }
            if ($("#bookAuthor").val() === "") {
                $('#errorBookAuthor').show().html('Edit a book author filed');
            }
            if ($("#bookFormat").val() === "") {
                $('#errorBookFormat').show().html('Edit a book format filed');
            }
            $('.errorMessages').css('display', 'table-row');
            $('.errorMessages').css('color', 'red');
            $('.errorMessages').css('font-size', '11px');
            return false;
        } else {
            alert("second");
            return true;
        }
    });
});

$(function() {
    $("#messageDialog").dialog({
        autoOpen: false,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            }
        }
    });

    $("#confirmationDialog").dialog({
        autoOpen: false,
        open: function() {
            setTimeout(function() {
                $('#dialog').dialog('close');
            }, 50000);
        },
        buttons: {
            "Delete": function(e) {
                $(this).dialog("close");
                var isChecked = false;
                for (var i = 0; i < $('.chkSelect').length; i++) {
                    if ($('.chkSelect').eq(i).prop("checked") === true) {
                        isChecked = true;
                    }
                }
                if (isChecked === true) {
                    $('form:first').submit();
                } else {
                    $("#messageDialog").dialog("open");
                    return false;
                }

            },
            Cancel: function(e) {
                $(this).dialog("close");
                return false;
            }
        }
    });

    $(".deleteButton").click(function(e) {
        $("#confirmationDialog").dialog("open");
        return false;
    });
});

